package medical.web.servlet;

import medical.database.DbUtil;
import medical.domain.menu.Menu;
import medical.domain.role.Role;
import medical.domain.user.User;

import java.io.IOException;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * 角色管理控制模块
 * @author 演示
 *
 */
public class RoleServlet extends BaseServlet {
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
/*		String m =req.getParameter("m");
		if("list".equals(m)){
			list(req, resp);
		}else if("add".equals(m)){
			add(req, resp);
		}else if("get".equals(m)){
			get(req, resp);
		}*/

		String servletPath = request.getServletPath();
		String methodName = servletPath.substring(1);
		methodName = methodName.substring(0,methodName.length()-3);

		try {
			//反射获取methName
			Method method=getClass().getDeclaredMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);
			//利用反射调用方法
			method.invoke(this,request, response);

		}catch(Exception e) {

			response.sendRedirect("error.jsp");
		}

	}

	//显示角色
	private void list(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException{

		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");


		List<Role> roles = new ArrayList<Role>(); // 创建一个list集合,用于装user集合
		Connection connection = DbUtil.getConn(); // 打开数据库连接
		String sql = "select * from t_role";

		try {
			PreparedStatement preparedStatement = connection.prepareStatement(sql);
			ResultSet rs = preparedStatement.executeQuery();

			while (rs.next()) {
				Integer id = rs.getInt("id");
				String roleid = rs.getString("roleid");
				String roleName = rs.getString("rolename");
				Role role = new Role();
				role.setId(id);
				role.setRoleid(roleid);
				role.setRolename(roleName);

				roles.add(role);
			}
			request.setAttribute("roles", roles);
			request.getRequestDispatcher("/page/role/role_list.jsp").forward(request, response);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		  
	}

//添加角色
	private void add(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException{

		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");

		String roleid = request.getParameter("roleid");
		String rolename = request.getParameter("rolename");

		Connection conn = DbUtil.getConn(); /// 获得数据库连接

		String sql = "insert into t_role values(null,?,?);";

		try {
			PreparedStatement prtmt = conn.prepareStatement(sql);
			prtmt.setString(1, roleid);
			prtmt.setString(2, rolename);

			int resInt = prtmt.executeUpdate();
			request.setAttribute("resInt", resInt);

			request.getRequestDispatcher("/list.Do").forward(request, response);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}


	//删除角色信息
	private void delete(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		Integer id = Integer.parseInt(request.getParameter("id"));
		if (id != null) {
			Connection conn = DbUtil.getConn();
			String sql = "delete from t_role where id = ?";
			try {
				PreparedStatement prtmt = conn.prepareStatement(sql);

				prtmt.setInt(1, id);
				int resInt = prtmt.executeUpdate();
				request.setAttribute("resInt", resInt);
				request.getRequestDispatcher("/list.Do").forward(request, response);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}


	//修改角色信息
	private void edit(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException, SQLException {

		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");

		Integer id = Integer.parseInt(request.getParameter("id"));
		Connection conn = DbUtil.getConn();
		String sql = "select * from t_role where id = " + id; // sql语句
		PreparedStatement preparedStatement = conn.prepareStatement(sql);
		ResultSet rs = preparedStatement.executeQuery();

		try {
			if (rs.next()) {

				Role role = new Role();

				role.setId(rs.getInt("id"));
				role.setRoleid(rs.getString("roleid"));
				role.setRolename(rs.getString("rolename"));

				request.setAttribute("role", role);
				request.getRequestDispatcher("/page/role/role_edit.jsp").forward(request, response);

			}
		}catch (SQLException e) {
			e.printStackTrace();
		}
	}

	//更新角色信息
	private void update(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");

		Integer id = Integer.parseInt(request.getParameter("id"));
		String roleid = request.getParameter("roleid");
		String rolename = request.getParameter("rolename");

		if (id != null && roleid != null && rolename != null) {

			Connection conn = DbUtil.getConn();

			String sql = "update t_role set roleid=?, rolename=? where id=" + id;

			try {
				PreparedStatement prtmt = conn.prepareStatement(sql);

				prtmt.setString(1, roleid);
				prtmt.setString(2, rolename);

				int resInt = prtmt.executeUpdate();

				request.setAttribute("resInt", resInt);

				request.getRequestDispatcher("/list.Do").forward(request, response);
			} catch (SQLException e) {
				e.printStackTrace();
			}

		}
	}


//角色功能权限
	private void role_list_menu(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException{

		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");


		List<Role> roles_list_menus = new ArrayList<Role>(); // 创建一个list集合,用于装user集合

		Set<Menu> menuSet = new HashSet<Menu>();

		Connection connection = DbUtil.getConn(); // 打开数据库连接
		/*select r.roleid, r.rolename, e.menuid, e.menuname, e.url, e.level from (t_role r, t_menu e) inner join t_role_menu rm on r.roleid = rm.roleid and e.menuid = rm.menuid*/
		String sql = "select r.roleid, r.rolename, e.menuid, e.menuname, e.url, e.level from (t_role r, t_menu e) inner join t_role_menu rm on r.roleid = rm.roleid and e.menuid = rm.menuid";

		try {
			PreparedStatement preparedStatement = connection.prepareStatement(sql);
			ResultSet rs = preparedStatement.executeQuery();

			while (rs.next()) {
				String roleid = rs.getString("roleid");
				String rolename = rs.getString("rolename");
				String menuid = rs.getString("menuid");
				String menuname = rs.getString("menuname");
				String url = rs.getString("url");
				Integer level = rs.getInt("level");

				Role role = new Role();
				role.setRoleid(roleid);
				role.setRolename(rolename);

				Menu menu = new Menu();

				menu.setMenuid(menuid);
				menu.setMenuname(menuname);
				menu.setUrl(url);
				menu.setLevel(level);

				menuSet.add(menu);

				role.setMenus(menuSet);

				roles_list_menus.add(role);
			}
			request.setAttribute("menus", menuSet);
			request.setAttribute("role_list_menus", roles_list_menus);
			request.getRequestDispatcher("/page/menu/role_list_menu.jsp").forward(request, response);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
